package com.jmz.apt.api.Provider;

import com.jmz.apt.api.Utils.MD5Util;
import com.jmz.apt.api.Utils.TimeUtil;
import com.jmz.apt.api.common.AptConstant;
import com.jmz.apt.api.entity.dbEntity.JmzAdminUser;
import com.jmz.apt.api.entity.dbEntity.TimeTygpGroup;
import com.jmz.apt.api.message.request.Condition;
import com.jmz.apt.api.message.request.Query;
import org.apache.ibatis.jdbc.SQL;

import java.util.Date;
import java.util.List;

public class TimeTygpGroupProvider extends BaseProvider {

    public String getCount(List<Condition> conditions) {
        return super.count(conditions);
    }

    public String select(String columns, List<Condition> conditions, Query query) {
        return  super.findAll(columns,conditions,query);
    }

    public String getTableName(){
        return "time_tygp_group";
    }


    public String insert(TimeTygpGroup timeTygpGroup) {
        SQL sql = new SQL();
        sql.INSERT_INTO(getTableName());
        if (timeTygpGroup.getName() != null) {
            sql.VALUES("name", "#{name}");
        }
        if (timeTygpGroup.getDsc() != null) {
            sql.VALUES("dsc", "#{dsc}");
        }

        Date now = new Date();
        timeTygpGroup.setCreateTime(now);
        timeTygpGroup.setUpdateTime(now);
        sql.VALUES("create_time", "#{createTime}");
        sql.VALUES("update_time", "#{updateTime}");
        String lastSql = sql.toString();
        System.out.println(lastSql);
        return lastSql;
    }

    public String selectOne(TimeTygpGroup timeTygpGroup) {
        SQL sql = new SQL();
        sql.SELECT("*");
        sql.FROM(getTableName());
        if (timeTygpGroup.getName() != null) {
            sql.WHERE("name like %#{name}% ");
        }
        if (timeTygpGroup.getId() != null) {
            sql.WHERE("id=#{name}");
        }
        String lastSql = sql.toString();
        System.out.println(lastSql);
        return lastSql + "limit 1";
    }

    public String delete(TimeTygpGroup timeTygpGroup) {
        return new SQL() {{
            DELETE_FROM(getTableName());
            WHERE("id = #{id}"); //暂时禁止批量删除
        }}.toString();
    }

    public String update(TimeTygpGroup timeTygpGroup) {
        return new SQL() {{
            UPDATE(getTableName());
            if (timeTygpGroup.getName() != null) {
                SET("name = #{name}");
            }
            if (timeTygpGroup.getDsc() != null) {
                SET("dsc = #{dsc}");
            }
            Date now = new Date();
            timeTygpGroup.setUpdateTime(now);
            SET("update_time=#{updateTime}");
            WHERE("id = #{id}");
        }}.toString();
    }
}
